import { request } from '../../request/index.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: [
      {
        id: 0,
        value: '综合',
        isActive: true
      },
      {
        id: 1,
        value: '销量',
        isActive: false
      },
      {
        id: 1,
        value: '价格',
        isActive: false
      }
    ],
    goodsList: [],
    goodsListPrice: []
  },
  goodsList: [],
  //搜索商品的名称
  goodsValue: '',
  //价格升序商品列表
  goodsListUp: [],
  //价格降序商品列表
  goodsListDown: [],
  //按照销量排序
  goodsListNum: [],
  //表示点击价格的时候  默认升序排列
  up: '',
  //点击销量的时候，默认升序
  numUp: '',
  //当前tab标题索引
  index: 0,
  //根据标题索引来激活选中标题数组
  changeTitleByIndex(index) {
    let { tabs } = this.data
    tabs.forEach((v, i) => i === index ? v.isActive = true : v.isActive = false)
    let i = tabs.findIndex(v => v.isActive === true)
    this.setData({
      tabs,
      index: i
    })
    //console.log(this.index)
  },
  handleTabsItemChange(e) {
    //console.log(e)
    const { index } = e.detail
    this.index = index
    this.changeTitleByIndex(index)
    //按照价格升降来搜索商品
    if (index === 0) {
      if (this.goodsValue !== '') {
        this.getGoodsList()
        console.log(this.goodsList)
        let goodsList = this.goodsList.filter(item => item.goodsName.indexOf(this.goodsValue) !== -1)
        this.setData({
          goodsList
        })
      } else {
        this.setData({
          goodsList: []
        })
      }
    } else if (index === 2) {
      if (this.up === '') {
        this.up = true
        console.log('价格升')
        console.log(this.goodsListUp)
        if (this.goodsValue !== '') {
          let goodsListUp = this.goodsListUp.filter(item => item.goodsName.indexOf(this.goodsValue) !== -1)
          this.setData({
            goodsListPrice: goodsListUp
          })
        } else {
          this.setData({
            goodsListPrice: []
          })
        }
      } else {
        this.up = !this.up
        if (this.up) {
          console.log('价格升')
          console.log(this.goodsListUp)
          if (this.goodsValue !== '') {
            let goodsListUp = this.goodsListUp.filter(item => item.goodsName.indexOf(this.goodsValue) !== -1)
            this.setData({
              goodsListPrice: goodsListUp
            })
          } else {
            this.setData({
              goodsListPrice: []
            })
          }
        } else {
          console.log('价格降')
          if (this.goodsValue !== '') {
            console.log(this.goodsListDown)
            let goodsListDown = this.goodsListDown.filter(item => item.goodsName.indexOf(this.goodsValue) !== -1)
            this.setData({
              goodsListPrice: goodsListDown
            })
          } else {
            this.setData({
              goodsListPrice: []
            })
          }
        }
      }
    } else {
      if (this.numUp === '') {
        this.numUp = false
        console.log('销量降序')
        if (this.goodsValue !== '') {
          console.log(this.goodsValue)
          let goodsListNum = this.goodsList.concat()
          goodsListNum = goodsListNum.sort(this.compare('goodsSalesVolume'))
          console.log(goodsListNum)
          //console.log(this.goodsList)
          goodsListNum = goodsListNum.filter(item => item.goodsName.indexOf(this.goodsValue) !== -1)
          this.setData({
            goodsListNum
          })
        }
      } else {
        this.numUp = !this.numUp
        if (this.numUp) {
          console.log('销量升序')
          if (this.goodsValue !== '') {
            console.log(this.goodsValue)
            let goodsListNum = this.goodsList.concat()
            goodsListNum = goodsListNum.sort(this.compareUp('goodsSalesVolume'))
            console.log(goodsListNum)
            //console.log(this.goodsList)
            goodsListNum = goodsListNum.filter(item => item.goodsName.indexOf(this.goodsValue) !== -1)
            this.setData({
              goodsListNum
            })
          } else {
            this.setData({
              goodsListNum: []
            })
          }

        } else {
          console.log('销量降序')
          if (this.goodsValue !== '') {
            console.log(this.goodsValue)
            let goodsListNum = this.goodsList.concat()
            goodsListNum = goodsListNum.sort(this.compare('goodsSalesVolume'))
            //console.log(this.goodsList)
            console.log(goodsListNum)
            goodsListNum = goodsListNum.filter(item => item.goodsName.indexOf(this.goodsValue) !== -1)
            this.setData({
              goodsListNum
            })
          } else {
            this.setData({
              goodsListNum: []
            })
          }
        }
      }
    }
  },
  //数组对象排序降序
  compare(property) {
    return function (a, b) {
      var value1 = a[property];
      var value2 = b[property];
      return value2 - value1;
    }
  },
  //数组对象排序升序
  compareUp(property) {
    return function (a, b) {
      var value1 = a[property];
      var value2 = b[property];
      return value1 - value2;
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //this.getGoodsListUp()
    //this.getGoodsListDown()
  },
  //获取所有商品列表
  async getGoodsList() {
    const res = await request({ url: '/fresh/goods/showList' })
    this.goodsList = res.data.data
    console.log(this.goodsList)
  },
  //获取所有商品的价格升序排序列表
  async getGoodsListUp() {
    const res = await request({ url: '/fresh/goods/showList', data: { orderType: 4 } })
    this.goodsListUp = res.data.data
  },
  //获取所有商品的价格降序排序列表
  async getGoodsListDown() {
    const res = await request({ url: '/fresh/goods/showList', data: { orderType: 3 } })
    this.goodsListDown = res.data.data
  },
  //搜索商品
  searchGoods(e) {
    this.goodsValue = e.detail
    if (e.detail !== '') {
      if (this.index === 0) {
        this.goodsValue = e.detail
        this.getGoodsList()
        console.log(this.goodsList)
        const value = e.detail
        let goodsList = this.goodsList.filter(item => item.goodsName.indexOf(value) !== -1)
        this.setData({
          goodsList
        })
      } else if (this.index === 2) {
        if (this.up) {
          console.log('价格升')
          console.log(this.goodsListUp)
          let goodsListUp = this.goodsListUp.filter(item => item.goodsName.indexOf(this.goodsValue) !== -1)
          this.setData({
            goodsListPrice: goodsListUp
          })
        } else {
          console.log('价格降')
          console.log(this.goodsListDown)
          let goodsListDown = this.goodsListDown.filter(item => item.goodsName.indexOf(this.goodsValue) !== -1)
          this.setData({
            goodsListPrice: goodsListDown
          })
        }
      } else {
        if (this.numUp) {
          console.log('销量升序')
          let goodsListNum = this.goodsList.concat()
          goodsListNum = goodsListNum.sort(this.compareUp('goodsSalesVolume'))
          //console.log(this.goodsList)
          console.log(goodsListNum)
          goodsListNum = goodsListNum.filter(item => item.goodsName.indexOf(this.goodsValue) !== -1)
          this.setData({
            goodsListNum
          })
        } else {
          console.log('销量降序')
          let goodsListNum = this.goodsList.concat()
          //console.log(this.goodsList)
          console.log(goodsListNum)
          goodsListNum = goodsListNum.sort(this.compare('goodsSalesVolume'))
          goodsListNum = goodsListNum.filter(item => item.goodsName.indexOf(this.goodsValue) !== -1)
          this.setData({
            goodsListNum
          })
        }
      }
    } else {
      this.setData({
        goodsList: [],
        goodsListPrice: [],
        goodsListNum: []
      })
    }
  },
  //跳转到商品详情页面
  goFood(e) {
    const goodsId = e.currentTarget.dataset.goodsid
    wx.navigateTo({
      url: '../food/index?goodsId=' + goodsId
    });
  },
  //添加商品到购物车
  handleAddToCart(e) {
    const goodsId = e.currentTarget.dataset.id
    const goodsIndex = this.goodsList.findIndex(v => v.goodsId === goodsId)
    const goodsObj = this.goodsList[goodsIndex]
    const item = {
      id: parseInt(goodsId),
      recommended: goodsObj.goodsPick ? 'recommended' : '',
      thumb: 'https://ma.yzlsoft.cn/static/' + goodsObj.goodsPictureMain,
      price: goodsObj.goodsPriceDiscount,
      originPrice: goodsObj.goodsPriceOriginal,
      volume: goodsObj.goodsSalesVolume,
      extra: '特价 限1份',
      num: goodsObj.goodsSalesVolume,
      title: goodsObj.goodsName,
      desc: goodsObj.goodsSummary
    }
    this.cart.addToCart(item)
  },
  //去支付
  handlePay() {
    const token = wx.getStorageSync("token");
    if (!token) {
      wx.navigateTo({
        url: '../login/index'
      })
    } else {
      wx.navigateTo({
        url: '../confirm-order/index'
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.cart = this.selectComponent('#cart')
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getGoodsListUp()
    this.getGoodsListDown()
    this.getGoodsList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})